package com.example.day1210_section_information.slice;

import com.example.day1210_section_information.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;


public class AdverSlice extends AbilitySlice {

    private Button mText;
    //
    private static final int TIMER = 1;
    // 计时器秒数
    private int TIMER_S = 1;

    private MyEvenHandle handle = new MyEvenHandle(EventRunner.create("jishi"));

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_abver_slice);

        initCompent();

// 第一次隔一秒后发送
        handle.sendEvent(TIMER, 1000);
    }

    /**
     * 引导页和主页的跳转
     */
    private void startMainAbility() {
        handle.removeAllEvent();
        handle = null;
//  创建一个轻量级数据库
        Preferences keyGuide = new DatabaseHelper(getContext()).getPreferences("key_guide");
        // 初始设置为false
        boolean showGuide = keyGuide.getBoolean("is_guide_show", false);
       // 当 showGuide 为 true 直接进入主页 否则进入引导页
        if (showGuide) {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName(getBundleName())
                    .withAbilityName(getBundleName() + ".MainAbility")
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        } else {
            present(new GuidePageSlice(), new Intent());
        }

        terminate();

    }

    /**
     * 初始化组件
     */
    private void initCompent() {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
        //设置状态栏的颜色
        getWindow().setStatusBarColor(Color.TRANSPARENT.getValue());

        mText = findComponentById(ResourceTable.Id_skip_ab);
        mText.setClickedListener(component -> startMainAbility());

    }

    /**
     * 每隔一秒自动发送一个计时请求
     * <p>
     * 3秒后 跳到引导页
     */
    private class MyEvenHandle extends EventHandler {

        public MyEvenHandle(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {

            super.processEvent(event);
            int eventId = event.eventId;

            if (eventId == TIMER) {
                TIMER_S--;
                getUITaskDispatcher().syncDispatch(() -> mText.setText(String.format("%ss 跳过", TIMER_S)));
                if (TIMER_S > 0) {
                    handle.sendEvent(TIMER, 1000);
                } else {
                    startMainAbility();
                }
            }

        }


    }
}

